/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package BUS;
import DTO.*;
import DAO.*;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
 *
 * @author ldtung_it
 */
public class MailBUS {
    /**
     * addMail into table Mail
     * @param mail
     * @return
     */
    public static int addMail(MailDTO mail){
        String error = "";
        if(!CheckData.isNum(mail.get_idMail())){
            error += "  IDMail must be number\n";
        }
        if(!CheckData.isNum(mail.get_idSender())){
            error += "  IDSender must be number\n";
        }
        if(!CheckData.isNum(mail.get_mailIn())){
            error += "  MailIn must be number\n";
        }
        if(!CheckData.isNum(mail.get_typeMail())){
            error += "  Type mail must be number\n";
        }
        if(mail.get_content() == null){
            error += "  Mail Content must not null\n";
        }
        if(mail.get_subject() == null){
            error += "  Mail subject must not null\n";
        }
        if(!CheckData.isDatetime(mail.get_datetimeSent())){
            error += "  Date of sending is not correct format\n";
        }
        if(error.length() != 0){
            JOptionPane.showMessageDialog(null, error);
            return 0;
        }
        try {
            int newCode = MailDAO.add(mail);
            return newCode;
        } catch (SQLException ex) {
            Logger.getLogger(MailBUS.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }
}
